/**
 * 
 */
package mblog.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import mblog.core.pojos.Attach;
import mblog.core.pojos.Comment;
import mblog.core.pojos.Post;
import mblog.core.pojos.Product;
import mblog.core.pojos.Tag;
import mblog.core.pojos.User;
import mtons.modules.pojos.UserProfile;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import com.faxsun.core.persist.entity.AttachPO;
import com.faxsun.core.persist.entity.CommentPO;
import com.faxsun.core.persist.entity.PostPO;
import com.faxsun.core.persist.entity.ProductRefPO;
import com.faxsun.core.persist.entity.TagPO;
import com.faxsun.core.persist.entity.UserPO;
import com.faxsun.core.pojos.enums.TagType;
import com.faxsun.core.readonly.entity.ProductRO;
import com.faxsun.pojos.view.CommentTable;
import com.faxsun.pojos.view.PostTable;
import com.faxsun.pojos.view.TagTable;
import com.faxsun.web.pojos.FSUserProfile;

/**
 * @author langhsu
 *
 */
@Component
public class BeanMapUtils {
	private static String[] USER_IGNORE = new String[] { "password", "mobile" };
	private static String[] POST_IGNORE = new String[] { "snapshot" };
	private static String[] POST_IGNORE_LIST = new String[] { "author",
			"snapshot", "markdown", "content" };
	private static String[] COMMENT_IGNORE = new String[] { "author" };
	public static MessageSource messageSource;

	public static User copy(UserPO po) {
		if (po == null) {
			return null;
		}
		User ret = new User();
		BeanUtils.copyProperties(po, ret, USER_IGNORE);
		return ret;
	}

	/**
	 * 将User实体类转换为passport实体
	 * 
	 * @param po
	 * @return
	 */
	public static UserProfile copyPassport(UserPO po) {
		UserProfile passport = new FSUserProfile(po.getId(), po.getUsername());
		passport.setName(po.getName());
		passport.setEmail(po.getEmail());
		passport.setAvatar(po.getAvatar());
		passport.setLastLogin(po.getLastLogin());
		passport.setStatus(po.getStatus());
		FSUserProfile fsPassport = (FSUserProfile) passport;
		fsPassport.setUserId(po.getUserId());
		fsPassport.setRoleLevel(po.getRoleLevel());
		fsPassport.setSignature(po.getSignature());
		return passport;
	}

	/**
	 * 将文章（Post）实体类转换为用于View的实体类
	 * 
	 * @param po
	 * @param level
	 *            {@link #POST_IGNORE},忽略封面图片；{@link #POST_IGNORE_LIST}
	 *            , 忽略作者、封面、markdown、内容
	 * @return
	 */
	public static Post copy(PostPO po, int level) {
		Post d = new Post();
		if (level > 0) {
			BeanUtils.copyProperties(po, d, POST_IGNORE);
		} else {
			BeanUtils.copyProperties(po, d, POST_IGNORE_LIST);
		}

		d.setType(po.getType());

		if (po.getAuthor() != null) {
			User u = new User();
			u.setId(po.getAuthor().getId());
			u.setUsername(po.getAuthor().getUsername());
			u.setName(po.getAuthor().getName());
			u.setAvatar(po.getAuthor().getAvatar());
			u.setSignature(po.getAuthor().getSignature());
			d.setAuthor(u);
		}
		if (po.getSnapshot() != null) {
			Attach a = new Attach();
			BeanUtils.copyProperties(po.getSnapshot(), a);
			d.setSnapshot(a);
		}
		if (po.getTagSets() != null) {
			StringBuffer sb = new StringBuffer();
			for (TagPO tag : po.getTagSets()) {
				sb.append(tag.getName());
				sb.append(",");
			}
			if (sb.length() > 0)
				sb.deleteCharAt(sb.length() - 1);
			d.setTags(sb.toString());
		}
		if (po.getDefaultTag() != null) {
			d.setProductTag(copy(po.getDefaultTag()));
		}
		return d;
	}

	public static Attach copy(AttachPO po) {
		Attach ret = new Attach();
		BeanUtils.copyProperties(po, ret);
		return ret;
	}

	public static Comment copy(CommentPO po) {
		Comment ret = new Comment();
		BeanUtils.copyProperties(po, ret, COMMENT_IGNORE);

		if (po.getAuthor() != null) {
			ret.setAuthor(copy(po.getAuthor()));
		}
		return ret;
	}

	/**
	 * Tag数据库实体类，转tagView
	 * 
	 * @param po
	 * @return
	 */
	public static Tag copy(TagPO po) {
		Tag ret = new Tag();
		BeanUtils.copyProperties(po, ret);

		ret.setType(TagType.parse(po.getType()));

		ProductRefPO productPO = po.getProductRef();
		if (productPO != null) {
			ret.setProduct(copy(productPO));
		}
		return ret;
	}

	/**
	 * ProductPO 转 Product
	 * 
	 * @param productPO
	 * @return
	 * @author songdragon
	 * @Date 2015-04-13 23:49
	 */
	public static Product copy(ProductRefPO productPO) {
		Product ret = new Product();
		if (productPO != null) {
			BeanUtils.copyProperties(productPO, ret);
			ret.setId(productPO.getProductId());
		}
		return ret;

	}

	/**
	 * Tag转TagTable，用于管理端展示标签
	 * 
	 * @param po
	 * @return
	 */
	public static TagTable copy(Tag po) {
		TagTable ret = new TagTable();
		BeanUtils.copyProperties(po, ret);

		ret.type = messageSource.getMessage(po.getType().name(), null,
				Locale.CHINA);

		ret.operation = "<a href='./add?tagId=" + ret.getId()
				+ "'>修改</a>&nbsp;" + "<a id='tag_delete_op' href='./delete/"
				+ ret.getId() + "'>删除</a>";
		return ret;
	}

	/**
	 * 将Post内容转换为适合表格控件显示的格式
	 * 
	 * @param po
	 * @return
	 */
	public static PostTable copy(Post po) {
		PostTable ret = new PostTable();

		BeanUtils.copyProperties(po, ret);

		ret.setAuthor(po.getAuthor().getName());
		ret.setAuthor_id(po.getAuthor().getId());

		ret.setOperation("<a href='./add?id=" + ret.getId() + "'>修改</a>&nbsp;"
				+ "<a id='post_delete_op' href='./delete/" + ret.getId()
				+ "' class='confirm'>删除</a>");
		ret.setType(messageSource.getMessage(po.getEnumType().name(), null,
				Locale.CHINA));

		switch (po.getStatus()) {
		case 0:
			ret.setStatusString("待审核");
			break;
		case 1:
			ret.setStatusString("已发布");
			break;
		case 2:
			ret.setStatusString("已删除");
			break;
		}
		return ret;
	}

	public static CommentTable copy(Comment po) {
		CommentTable ret = new CommentTable();

		BeanUtils.copyProperties(po, ret);

		ret.setOperation("<a id='comment_delete_op' href='./delete/"
				+ ret.getId() + "' class='confirm'>删除</a>");

		String url = "";
		switch (po.getType()) {
		// FIXME:improve code---use template string and stringbuffer
		case 0:
			// 对产品评论
			url = "<a href='/faxsun-info/product/view/" + po.getToId()
					+ "' target='_blank'>" + po.getToId() + "</a>";

			break;
		case 1:
			// 对文章评论
			url = "<a href='/faxsun-info/post/view/" + po.getToId()
					+ "' target='_blank'>" + po.getToId() + "</a>";
			break;
		}
		ret.setToContent(url);

		switch (po.getStatus()) {
		case 0:
			ret.setStatus("待审核");
			break;
		case 1:
			ret.setStatus("已发布");
			break;
		case 2:
			ret.setStatus("已删除");
			break;
		}
		return ret;
	}

	/**
	 * 将TagPO list转为Tag List
	 * 
	 * @param tagpos
	 * @return
	 */
	public static List<Tag> copy(List<TagPO> tagpos) {
		List<Tag> ret = new ArrayList<Tag>();
		if (tagpos == null || tagpos.size() == 0) {
		} else {
			for (TagPO tagpo : tagpos) {
				ret.add(copy(tagpo));
			}
		}
		return ret;
	}

	public static List<Product> copy(List<TagPO> tagpos, int type) {
		List<Product> ret = new ArrayList<Product>();
		if (tagpos == null || tagpos.size() == 0) {
		} else {
			for (TagPO tagpo : tagpos) {
				Product product = copy(tagpo.getProductRef());
				product.setTag(copy(tagpo));
				ret.add(product);
			}
		}
		return ret;
	}

	public static ProductRefPO copy(ProductRO productRO) {
		ProductRefPO ret = new ProductRefPO();

		ret.setProductId(productRO.getId());
		ret.setIsFaxsun(1);
		ret.setUrl(productRO.getUrl());
		return ret;
	}

	public static List<Post> copyPostList(List<PostPO> posts) {

		return null;
	}

	@Autowired(required = true)
	public void setMessageSource(MessageSource messageSource) {
		BeanMapUtils.messageSource = messageSource;
	}
}
